package team.se.tms.factory;

import org.springframework.web.multipart.MultipartFile;
import team.se.tms.constant.PathConstant;
import team.se.tms.po.*;
import team.se.tms.util.EncryptUtil;
import team.se.tms.util.FileUtil;
import team.se.tms.util.KeyUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 数据模型工厂
 *
 * @author winkyle
 * @date 2018/12/5
 */
public class ModelFactory {
    /**
     * 根据角色id生成默认用户信息类
     *
     * @param roleId 用户角色id
     * @return 用户信息类
     * @create on 2018/12/5 by winkyle
     */
    public static UserInfo createUserInfo(String roleId){
        String userId = KeyUtil.generatePrimaryKey();
        String password = EncryptUtil.sha1("123456",userId);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setUserPassword(password);
        userInfo.setUserHeadPath("avatar/default.jpg");
        userInfo.setUserSkin("no-skin");
        userInfo.setRoleId(roleId);
        return userInfo;
    }


    /**
     * 根据用户信息生成用户信息类
     *
     * @return 用户信息类
     * @create on 2018/12/31 by qingx
     */
//    public static UserInfo createUserInfo(UserInfo userInfo,MultipartFile multipartFile){
//        if(multipartFile != null){
//            String path =  PathConstant.HEAD_PATH;
//            String fileName = userInfo.getUserId()+FileUtil.getExtension(multipartFile.getOriginalFilename());
//            String address = path+fileName;
//            FileUtil.storeFile(path, fileName, multipartFile);
//            userInfo.setUserHeadPath(address);
//        }else{
//            userInfo.setUserHeadPath("avatar/default.jpg");
//        }
//        userInfo.setUserSkin("no-skin");
//        return userInfo;
//    }


    /**
     * 生成课程资料信息类
     *
     * @param multipartFile 文件
     * @param createTime 创建时间
     * @return 课程资料信息类
     * @create on 2018/12/7 by winkyle
     */
    public static CourseMaterialInfo createCourseMaterialInfo(MultipartFile multipartFile, Date createTime){
        String courseMaterialId = KeyUtil.generatePrimaryKey();
        String name = FileUtil.GetFileNameWithoutExtension(multipartFile.getOriginalFilename());
        String path =  PathConstant.COURSE_MATERIAL;
        String fileName = courseMaterialId+FileUtil.getExtension(multipartFile.getOriginalFilename());
        String address = path+fileName;
        FileUtil.storeFile(path, fileName, multipartFile);
        CourseMaterialInfo courseMaterialInfo = new CourseMaterialInfo();
        courseMaterialInfo.setCourseMaterialId(courseMaterialId);
        courseMaterialInfo.setCourseMaterialName(name);
        courseMaterialInfo.setCourseMaterialAddress(address);
        courseMaterialInfo.setCourseMaterialCreateTime(createTime);
        return courseMaterialInfo;
    }

    /**
     * 生成授课-课程资料信息类
     *
     * @param teachCourseId 授课id
     * @param courseMaterialId 课程资料
     * @param createTime 创建时间
     * @return 授课-课程资料信息类id
     * @create on 2018/12/7 by winkyle
     */
    public static TeachCourse_CourseMaterialInfo createTeachCourse_CourseMaterialInfo(String teachCourseId, String courseMaterialId, Date createTime) {
        TeachCourse_CourseMaterialInfo teachCourse_courseMaterialInfo = new TeachCourse_CourseMaterialInfo();
        teachCourse_courseMaterialInfo.setTeachCourseId(teachCourseId);
        teachCourse_courseMaterialInfo.setCourseMaterialId(courseMaterialId);
        teachCourse_courseMaterialInfo.setTeachCourseMaterialCreateTime(createTime);
        return teachCourse_courseMaterialInfo;
    }

    /**
     * 上传人才培养方案，并生成人才培养方案信息类
     *
     * @param multipartFile 文件
     * @return TalentTrainingProgramInfo 才培养方案信息类
     * @throws Exception 异常
     * @create on 2018/12/17 by johnny
     */
    public static TalentTrainingProgramInfo createTalentProgramInfo(MultipartFile multipartFile){
        //生成主键
        String talentTrainingProgramId = KeyUtil.generatePrimaryKey();
        //获取文件名
        String name = FileUtil.GetFileNameWithoutExtension(multipartFile.getOriginalFilename());
        //文件二级路径
        String path =  PathConstant.TALENT_TRAINING_PROGRAM;
        //生成储存的文件名
        String fileName = talentTrainingProgramId+FileUtil.getExtension(multipartFile.getOriginalFilename());
        //生成文件地址
        String address = path+fileName;
        //存储文件
        FileUtil.storeFile(path, fileName, multipartFile);


        TalentTrainingProgramInfo talentTrainingProgramInfo = new TalentTrainingProgramInfo();
        talentTrainingProgramInfo.setTalentTrainingProgramName(name);
        talentTrainingProgramInfo.setTalentTrainingProgramId(talentTrainingProgramId);
        talentTrainingProgramInfo.setTalentTrainingProgramAddress(address);
        return talentTrainingProgramInfo;
    }

    /**
     * 上传作业，并返回生成的作业类
     * @create on 2018/12/29 by argentum
     *
     * @param assignmentInfo 不包含文件的作业信息
     * @param multipartFile 文件类
     * @return
     */
    public static AssignmentInfo packageAssignmentInfo(AssignmentInfo assignmentInfo,MultipartFile multipartFile,int flag){
        String assignmentId = "";
        Date date = new Date();
        if(flag==0){
            //修改作业
            System.out.println("getAssignmentId===========update"+assignmentInfo.getAssignmentId());
            assignmentId = assignmentInfo.getAssignmentId();
            assignmentInfo.setAssignmentUpdateTime(date);
        }
        if(flag==1){
            //创建作业，生成主键
            assignmentId = KeyUtil.generatePrimaryKey();
            assignmentInfo.setAssignmentId(assignmentId);
            assignmentInfo.setAssignmentCreateTime(date);
            assignmentInfo.setAssignmentState(0);
            assignmentInfo.setAssignmentUpdateTime(date);
        }

        //获取文件名
        String name = FileUtil.GetFileNameWithoutExtension(multipartFile.getOriginalFilename());
        //文件二级路径
        String path = PathConstant.ASSIGNMENT;
        //生成储存的文件名
        String fileName = assignmentId+FileUtil.getExtension(multipartFile.getOriginalFilename());
        //生成文件地址
        String address = path+fileName;

        //存储文件
        FileUtil.storeFile(path, fileName, multipartFile);

        assignmentInfo.setAssignmentName(name);
        assignmentInfo.setAssignmentAddress(address);

        return assignmentInfo;
    }

    public static List<GroupInfo> createGroup(List<String> students,String projectId,String groupLeader){
        List<GroupInfo> groupInfos = new ArrayList<>();
        for(int i = 0;i<students.size();i++){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setProjectId(projectId);
            groupInfo.setStuId(students.get(i));
            if(students.get(i).equals(groupLeader)){
                groupInfo.setGroupIdentity(0);
            }else{
                groupInfo.setGroupIdentity(1);
            }
            groupInfos.add(groupInfo);
        }
        return groupInfos;
    }

}
